package com.example.springboot.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.example.springboot.controller.dto.LoginDTO;
import com.example.springboot.controller.request.BaseRequest;
import com.example.springboot.controller.request.LoginRequest;
import com.example.springboot.controller.request.PasswordRequest;
import com.example.springboot.entity.Admin;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.mapper.AdminMapper;
import com.example.springboot.service.IAdminService;
import com.example.springboot.utils.TokenUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminService implements IAdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public List<Admin> list() {
        return adminMapper.list();
    }

    @Override
    public PageInfo<Admin> page(BaseRequest baseRequest) {
        PageHelper.startPage(baseRequest.getPageNum(), baseRequest.getPageSize());
        List<Admin> admins = adminMapper.listByCondition(baseRequest);
        return new PageInfo<>(admins);
    }

    @Override
    public Admin getById(String username) {
        return adminMapper.getById(username);
    }

    @Override
    public void add(Admin admin) {
        Date date = new Date();
        admin.setId(Math.abs(DateUtil.format(date,"yyyMMdd").hashCode()));
//        admin.setId(DateUtil.format(date,"yyyMMdd") + IdUtil.fastSimpleUUID());       日期+一个UID .hashCode()变成纯数字
//        admin.setId(DateUtil.format(date,"yyyMMdd") + IdUtil.fastSimpleUUID());       日期+一个UID
        adminMapper.add(admin);
    }

    @Override
    public void delete(Admin admin) {
        adminMapper.delete(admin);
    }

    @Override
    public LoginDTO login(LoginRequest request) {
            Admin admin = adminMapper.getByUsernameAndPassword(request.getUsername(),request.getPassword());
            if(admin==null){
                throw new ServiceException("用户名或密码错误");
            }
            if(admin.isStatus() == false){
                throw new ServiceException("该用户处于禁用状态");
            }
            LoginDTO loginDTO = new LoginDTO();
            BeanUtils.copyProperties(admin,loginDTO);

        //生成token
        String token = TokenUtils.genToken(String.valueOf(admin.getId()), admin.getPassword());
        loginDTO.setToken(token);

        return loginDTO;
    }

    @Override
    public void changePass(PasswordRequest request) {
//        对新的密码进行加密
        int count = adminMapper.updatePassword(request);
        if(count <= 0) {
            throw new ServiceException("用户名或密码错误");
        }
    }

    @Override
    public void update(Admin obj) {
        adminMapper.updateById(obj);
    }


}
